﻿using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Web.Areas.SysAdmin.Models;
using lzwlz.Web;
using System;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.Mall.Controllers
{
    public class LoginController : BaseController
    {
        #region 界面
        /// <summary>
        /// 登录界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region 接口
        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult MemberLogin(string userName, string passWord)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var memberService = new MemberService();
                var result = memberService.Login(userName, passWord, out MemberLogin model);

                if (result.Success)
                {
                    memberService.UpdateLoginTime(model.Id);//更新登录时间

                    int KeepHours = 1;//默认一个小时过期

                    // 写入用户身份验证票证
                    MallMemberLogin.SignIn(model, KeepHours * 60);
                    response.Data = Url.RouteUrl(new { area = "Mall", controller = "Home", action = "Index" });

                    response.Success = true;
                    response.Message = "登录成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = result.Message;
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        #endregion
    }
}